package com.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dao.AreaIpDao;
import com.enetiy.Areaip;
import com.enetiy.Areaitemip;
import com.enetiy.Push;

public class AreaIpDaoImpl extends HibernateDaoSupport implements AreaIpDao {
	@Override
	public Serializable insertAreaIp(Areaip areaip) throws Exception {
		return this.getHibernateTemplate().save(areaip);
	}

	@Override
	public Areaip selectAreaIp(String publicIp) throws Exception {
		Query query = this.getSession().createQuery("from Areaip where public_ip=?");
		query.setParameter(0, publicIp);
		List<Areaip> list = query.list();
		return list.isEmpty()?null:list.get(0);
	}
	@Override
	public Areaip selectAreaip(int areaid) throws Exception {
		Query query = this.getSession().createQuery("from Areaip where area_id=?");
		query.setParameter(0, areaid);
		List<Areaip> list = query.list();
		return list.isEmpty()?null:list.get(0);
	}

	@Override
	public void updateAreaIp(Areaip areaip) throws Exception {
		this.getHibernateTemplate().update(areaip);
	}

	@Override
	public Serializable insertAreaitemip(Areaitemip areaitemip)
			throws Exception {
		return this.getHibernateTemplate().save(areaitemip);
	}

	@Override
	public void updateAreaitemip(Areaitemip areaitemip) throws Exception {
		this.getHibernateTemplate().update(areaitemip);
	}

	@Override
	public Areaitemip selectAreaitemip(String keyword, int areaid) throws Exception {
		String sql= "from Areaitemip where areaid=? and keyword =?";
		int iden = 0;
		if(keyword==null||keyword.equals("")){
			sql= "from Areaitemip where areaid=?";
			iden=1;
		}
		if(areaid==0){
			sql= "from Areaitemip where keyword=?";
			iden=2;
		}
		
		Query query = this.getSession().createQuery(sql);
		if(iden==1)
			query.setParameter(0, areaid);
		if(iden==2)
			query.setParameter(0, keyword);
		if(iden==0){
			query.setParameter(0, areaid);
			query.setParameter(1, keyword);
		}
		List<Areaitemip> list = query.list();
		return list.isEmpty()?null:list.get(0);
	}

	@Override
	public List<Areaitemip> selectAreaitemipList() throws Exception {
		Query query = this.getSession().createQuery("from Areaitemip");
		return query.list();
	}

	@Override
	public List<Areaip> selectAreaipList() throws Exception {
		return this.getSession().createQuery("from Areaip").list();
	}

	@Override
	public List<Push> selectPushList(String use, int type) throws Exception {
		Query query = this.getSession().createQuery("from Push where push_use = ? and push_type = ?");
		query.setParameter(0, use);
		query.setParameter(1,type);
		List<Push> list = query.list();
		return list;
	}
}
